package com.ophiux.cs.application.consultBase.impl;

import java.util.List;

import com.ophiux.cs.application.core.ResponsePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.consultBase.dto.AddDeptRequest;
import com.ophiux.cs.application.consultBase.dto.AddDeptResponse;
import com.ophiux.cs.application.consultBase.dto.AddHospitalRequest;
import com.ophiux.cs.application.consultBase.dto.AddHospitalResponse;
import com.ophiux.cs.application.consultBase.dto.GetDeptRequest;
import com.ophiux.cs.application.consultBase.dto.GetDeptResponse;
import com.ophiux.cs.application.consultBase.dto.deptCreateVadilRequest;
import com.ophiux.cs.application.consultBase.service.IAddDeptAppService;
import com.ophiux.cs.application.consultBase.service.IGetDeptAppService;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.Throws.ParamException;
import com.ophiux.cs.domain.consultBase.model.entity.Dept;
import com.ophiux.utils.common.ShiftUtils;
import com.ophiux.utils.parser.json.JsonUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 科室综合信息服务
 * @author ADMIN
 *
 */
@Service
@Slf4j
public class DeptGeneralService {
	@Autowired
	IGetDeptAppService iGetDeptAppService;//查询科室信息服务
	@Autowired
	IAddDeptAppService iAddDeptAppService;//新增科室信息服务
	
	public Dept deptCreateVadil(deptCreateVadilRequest dto) throws ParamException {
		Dept deptInfo = null;
		dto.verParam();
		
		GetDeptRequest getDeptDto = new GetDeptRequest();
		getDeptDto.setDEPT_NAME(dto.getDEPT_NAME());
		Result<ResponsePage<GetDeptResponse>> deptResult = iGetDeptAppService.getDept(getDeptDto);
		if(deptResult != null) {
			if(deptResult.getRESULT().equals(Result.RESULT_SUCCESS)) {
				if(deptResult.getRESPONSE() != null && deptResult.getRESPONSE().getData().size() != 0) {
					GetDeptResponse getDeptRes = deptResult.getRESPONSE().getData().get(0);
					
					deptInfo = (Dept) ShiftUtils.objToNewObj(getDeptRes, Dept.class);
					log.info("【deptCreateVadil】已经存在该科室信息，暂不处理本种情况。当前科室名称："+dto.getDEPT_NAME());
				}else {
					deptInfo = new Dept();
					
					AddDeptRequest addDeptDto = new AddDeptRequest();
					addDeptDto.setDEPT_NAME(dto.getDEPT_NAME());
					addDeptDto.setHOSPITAL_ID(dto.getHOSPITAL_ID());
					Result<AddDeptResponse> addDeptResult = iAddDeptAppService.createDept(addDeptDto);
					if(addDeptResult != null && addDeptResult.getRESULT().equals(Result.RESULT_SUCCESS)) {
						log.info("【deptCreateVadil】新增科室成功。当前科室名称："+dto.getDEPT_NAME());
						
						deptInfo.setDEPT_ID(addDeptResult.getRESPONSE().getDEPT_ID());
						deptInfo.setHOSPITAL_ID(dto.getHOSPITAL_ID());
						deptInfo.setDEPT_NAME(dto.getDEPT_NAME());
					}else {
						log.info("【deptCreateVadil】新增科室失败。当前科室名称："+dto.getDEPT_NAME()+"。返回信息为:"+JsonUtils.toJson(addDeptResult));
					}
				}
			}else {
				log.info("【deptCreateVadil】获取科室信息未知错误，暂不处理本种情况。当前科室名称："+dto.getDEPT_NAME()+"。返回信息为:"+JsonUtils.toJson(deptResult));
			}
		}else {
			log.info("【deptCreateVadil】获取科室信息未得到返回信息，暂不处理本种情况。当前科室名称："+dto.getDEPT_NAME());
		}
		return deptInfo;
	}
	
}
